<script setup lang='ts'>
  import { reactive, ref } from 'vue';
  import { useRouter } from 'vue-router';
  import { getPassword } from "@/utils/loginPassWord";
  import { userStore } from '@/pinia/modules/user';
  const useuserStore = userStore();
  const { Login } = useuserStore;
  const $router = useRouter();
  const fomrRef = ref();
  const loading = ref(false);
  const loginForm = reactive({
    username: null,
    password: null,
  })
  const manageName = import.meta.env.VITE_MANAGE_NAME
  const validateUsername = (rule: any, value: any, callback: (arg0: Error | undefined) => void) => {
    if (!value) {
      callback(new Error("请输入用户名"));
    } else {
      callback(undefined);
    }
  };
  const validatePass = (rule: any, value: string | any[], callback: (arg0: Error | undefined) => void) => {
    if (!value) {
      callback(new Error("请输入密码"));
    } else {
      if(value.length < 5) {
        callback(new Error("密码不能小于5位"));
      }else {
        callback(undefined);
      }
    }
  };
  const loginRules = {
    username: [
      { required: true, trigger: "blur", validator: validateUsername },
    ],
    password: [
      { required: true, trigger: "blur", validator: validatePass },
    ],
  };
  const handleLogin = ()=> {
    fomrRef.value.validate((valid: any) => {
      if (valid) {
        let dataInfo = JSON.parse(JSON.stringify(loginForm));
        dataInfo.password = getPassword(dataInfo.password);
        loading.value = true;
        Login(dataInfo).then(() => {
          loading.value = false;
          $router.push({ path: "/" });
        }).catch(() => {
          loading.value = false;
        });
      } else {
        return false;
      }
    });
  }
</script>

<template>
  <div class="login-container">
    <el-form
      ref="fomrRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      label-position="left"
    >
      <div class="title">{{ manageName }}</div>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          prefix-icon="User"
          placeholder="账号"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          class="text"
          v-model="loginForm.password"
          prefix-icon="Lock"
          show-password
          clearable
          placeholder="密码"
          @change="handleLogin"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          @click="handleLogin"
        >
          登陆
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang='scss' scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;
.login-container {
  min-height: 100vh;
  padding: 120px 0 0;
  box-sizing: border-box;
  background-color: $bg;
  :deep(.login-form) {
    width: 420px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    box-sizing: border-box;
    margin: 0 auto;
    .el-input__wrapper {
      height: 47px;
      box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1) inset;
      background: rgba(0, 0, 0, 0.1);
    }
    input{
      height: 100%;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
    .el-form-item__content {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
    }
    .el-input {
      --el-input-text-color:#fff;
    }
    .el-button {
      width: 100%;
      height: 37px;
    }
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px;
    text-align: center;
    font-weight: bold;
  }
}
</style>